//
//  ForgetView.m
//  eticket
//
//  Created by AnhQuocNguyen on 4/18/13.
//  Copyright (c) 2013 SoinMedia. All rights reserved.
//

#import "ForgetView.h"

@implementation ForgetView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


- (void)dealloc {
    [_txtEmail release];
    [_btnSubmit release];
    [super dealloc];
}

#pragma mark - MBProgressHUD
-(void)initHUD:(NSString *)alert {
    if (mHud) {
        [mHud removeFromSuperview];
        SAFE_RELEASE_OBJECT(mHud);
    }
    mHud = [[MBProgressHUD alloc] initWithView:self];
    //    mHud.delegate = self;
    mHud.labelText = alert;
    mHud.dimBackground = YES;
    [self addSubview:mHud];
    
}
-(void)waitting{
    while (mLoading) {
        usleep(5000);
        if (_countTimeout >= 187*20 /* 187 ~ 3s ==>> 187 *20 ~ 1' */) {
            mLoading = NO;
            
        }
    }
}

#pragma mark - setupForgetPassword
- (void)setupForgetPassword{
    _txtEmail.delegate = self;
}

#pragma mark - Textfield Delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    remClickText = textField;
    NSLog(@"textFieldShouldBeginEditing");
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - alertTextField
- (void)alertTextField:(NSString *)message{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:message delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    alert.tag = ALERT_MESSAGE;
    [alert show];
    SAFE_RELEASE_OBJECT(alert);
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == ALERT_MESSAGE) {
        [self textFieldbecomeFirstResponder];
    }
}


#pragma mark - Become & Resign textfield
- (void)textFieldResignFirstResponder{
    if (remClickText && [remClickText isFirstResponder]) {
        [remClickText resignFirstResponder];
    }
}
- (void)textFieldbecomeFirstResponder{
    if ([remClickText canBecomeFirstResponder]) {
        [remClickText becomeFirstResponder];
    }
}

#pragma mark - TouchEvent
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [remClickText resignFirstResponder];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    [remClickText resignFirstResponder];
}


#pragma mark - btnBackReturn_Clicked
- (IBAction)btnBackReturn_Clicked:(id)sender{
    [self textFieldResignFirstResponder];
    if ([_forgetViewDelegate respondsToSelector:@selector(forgetViewWithBack:)]) {
        [_forgetViewDelegate forgetViewWithBack:nil];
    }
}

#pragma mark - btnSubmitForgetPassword_Clicked
- (IBAction)btnSubmitForgetPassword_Clicked:(id)sender{
    NSString *strEmail = [Utilities trimString:_txtEmail.text];
    if ([Utilities isEmptyString:strEmail] || ![Utilities isValidEmail:strEmail]) {
        remClickText = _txtEmail;
        [self alertTextField:@"Email is invalid !!!"];
        return;
    }
    
    [self textFieldResignFirstResponder];
    
    
    //Submit to serer
    
    mLoading = YES;
    [self initHUD:@"Submitting...."];
    [mHud showWhileExecuting:@selector(waitting) onTarget:self withObject:nil animated:NO];
    
    NSString *urlString = [NSString stringWithFormat:@"%@%@%@", URL_island_API, forgotPassword_API, TOKEN__island];
    NSURL *url = [NSURL URLWithString:urlString];
    __block ASIFormDataRequest *requestServer = [ASIFormDataRequest requestWithURL:url];
    [requestServer setTimeOutSeconds:20];
    [requestServer addPostValue:[Utilities trimString:_txtEmail.text] forKey:@"email"];
    
    //////////////////////////////////////////////////////////////////
    [requestServer setCompletionBlock:^{
        mLoading = NO;
        NSError *error = nil;
        NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:[requestServer responseData] options:kNilOptions error:&error];
        NSLog(@"%@", jsonData);
        
        //Check error server
        if (error) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:[error description] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
            [alert show];
            SAFE_RELEASE_OBJECT(alert);
            return;
        }
        
        //Check error json
        NSDictionary *jsonError = [jsonData objectForKey:@"error"];
        if ([[jsonError objectForKey:@"code"] intValue] != 200) {
            NSArray *messageArray = [jsonError objectForKey:@"message"];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:[messageArray objectAtIndex:0] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
            [alert show];
            SAFE_RELEASE_OBJECT(alert);
            return;
        }
    
        NSString *messageArray = [jsonError objectForKey:@"message"];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:messageArray delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        SAFE_RELEASE_OBJECT(alert);
        
        _txtEmail.text = @"";
    }];
    
    //////////////////////////////////////////////////////////////////
    [requestServer setFailedBlock:^{
        mLoading = NO;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Submit faild !!!" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
        [alert show];
        SAFE_RELEASE_OBJECT(alert);
    }];
    [requestServer startSynchronous];
}
@end
